# Contributor: Frederic Wang <fred.wang@free.fr>

_realname=py
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=1.5.3
pkgrel=1
pkgdesc='library with cross-python path, ini-parsing, io, code, log facilities (mingw-w64)'
arch=('any')
url='https://pylib.readthedocs.io/'
license=('MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-python2"
                        "${MINGW_PACKAGE_PREFIX}-python3"
                        "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
                        "${MINGW_PACKAGE_PREFIX}-python2-setuptools")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python2-pytest"
              "${MINGW_PACKAGE_PREFIX}-python3-pytest")
options=('staticlibs' 'strip' '!debug')
source=("${_realname}-${pkgver}.tar.gz::https://github.com/pytest-dev/py/archive/${pkgver}.tar.gz")
sha256sums=('c0698a6f71e9ea2a44e5bdf81f1ec8d5b5e9646e990c75fadeb133149aeb8440')

prepare() {  
  cd "${srcdir}"
  #work around tar issue with symlinks
  #tar xf ${_realname}-${pkgver}.tar.gz || true
  cp ${_realname}-${pkgver}/LICENSE ${_realname}-${pkgver}/py/LICENSE
  rm -rf $builddir | true
  for builddir in python{2,3}-build-${CARCH}; do  
    rm -rf ${builddir} | true
    cp -r "${_realname}-${pkgver}" "${builddir}"
  done  
}

# Note that build() is sometimes skipped because it's done in 
# the packages setup.py install for simplicity if you can do so.
# but sometimes, you want to do a check before install which would
# also trigger the build.  
build() {  
  for pver in {2,3}; do  
    msg "Python ${pver} build for ${CARCH}"  
    cd "${srcdir}/python${pver}-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} setup.py build
  done  
}

  
check() {  
  for pver in {2,3}; do  
    msg "Python ${pver} test for ${CARCH}"  
    cd "${srcdir}/python${pver}-build-${CARCH}"  
    ${MINGW_PREFIX}/bin/python${pver} setup.py check 
  done  
}

package_python3-py() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3")

  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/LICENSE"
}

package_python2-py() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2")

  cd "${srcdir}/python2-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/LICENSE"
}

package_mingw-w64-i686-python2-py() {
  package_python2-py
}

package_mingw-w64-i686-python3-py() {
  package_python3-py
}

package_mingw-w64-x86_64-python2-py() {
  package_python2-py
}

package_mingw-w64-x86_64-python3-py() {
  package_python3-py
}
